﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CLManagement
{
    public class Management
    {
        /// <summary>
        /// 文件路径
        /// </summary>
        public string jsonPath;
        public Management()
        {

        }
        public Management(string path)
        {
            this.jsonPath = path;
        }
        /// <summary>
        /// 加载json
        /// </summary>
        /// <returns></returns>
        public JObject LoadJsonFromFile()
        {
            try
            {
                string jsonText = File.ReadAllText(this.jsonPath, Encoding.UTF8);
                JObject keyValuePairs = JObject.Parse(jsonText);
                return keyValuePairs;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return null;
            }
        }
        /// <summary>
        /// 从本地加载json文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public JObject LoadJsonFromFile(string path)
        {
            this.jsonPath = path;
            try
            {
                string jsonText = File.ReadAllText(path, Encoding.UTF8);
                JObject keyValuePairs = JObject.Parse(jsonText);
                return keyValuePairs;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return null;
            }
        }
        /// <summary>
        /// 保存json文件到本地
        /// </summary>
        /// <param name="jsonObject"></param>
        /// <param name="filePath"></param>
        public void SaveJsonToFile(JObject jsonObject, string filePath)
        {
            
            string jsonData = jsonObject.ToString(Newtonsoft.Json.Formatting.Indented);
            File.WriteAllText(filePath, jsonData);
        }


        /// <summary>
        /// 添加新键值
        /// </summary>
        /// <param name="keyName">新的键（a.b.c）</param>
        /// <param name="value">值</param>
        public void AddKeyValue(string keyName, JToken value)
        {
            JObject jsonData = LoadJsonFromFile(this.jsonPath);
            try
            {
                // Split the path to find the parent object
                string[] keys = keyName.Split('.');
                JObject current = jsonData;

                // Navigate to the object that will contain the new key
                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else
                    {
                        // If any segment of the path does not exist, create new JObject containers
                        current[keys[i]] = new JObject();
                        current = (JObject)current[keys[i]];
                    }
                }

                // Add the new key with the provided value
                string newKey = keys[keys.Length - 1];
                if (!current.ContainsKey(newKey))
                {
                    current[newKey] = value;  // Directly add the string value
                    this.SaveJsonToFile(jsonData, this.jsonPath);
                }
                else
                {
                    MessageBox.Show($"Key '{newKey}' 已经存在");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败:" + ex.Message);
            }
        }
        public void AddKeyValue(string keyName, string value)
        {
            JObject jsonData = LoadJsonFromFile(this.jsonPath);
            try
            {
                // Split the path to find the parent object
                string[] keys = keyName.Split('.');
                JObject current = jsonData;

                // Navigate to the object that will contain the new key
                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else
                    {
                        // If any segment of the path does not exist, create new JObject containers
                        current[keys[i]] = new JObject();
                        current = (JObject)current[keys[i]];
                    }
                }

                // Add the new key with the provided value
                string newKey = keys[keys.Length - 1];
                if (!current.ContainsKey(newKey))
                {
                    current[newKey] = value;  // Directly add the string value
                    this.SaveJsonToFile(jsonData, this.jsonPath);
                }
                else
                {
                    MessageBox.Show($"Key '{newKey}' 已经存在");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败:" + ex.Message);
            }
        }
        public void AddKeyValue(string keyName, int value)
        {
            JObject jsonData = LoadJsonFromFile(this.jsonPath);
            try
            {
                // Split the path to find the parent object
                string[] keys = keyName.Split('.');
                JObject current = jsonData;

                // Navigate to the object that will contain the new key
                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else
                    {
                        // If any segment of the path does not exist, create new JObject containers
                        current[keys[i]] = new JObject();
                        current = (JObject)current[keys[i]];
                    }
                }

                // Add the new key with the provided value
                string newKey = keys[keys.Length - 1];
                if (!current.ContainsKey(newKey))
                {
                    current[newKey] = value;  // Directly add the string value
                    this.SaveJsonToFile(jsonData, this.jsonPath);
                }
                else
                {
                    MessageBox.Show($"Key '{newKey}' 已经存在");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败:" + ex.Message);
            }
        }
        public void AddKeyValue(string keyName, float value)
        {
            JObject jsonData = LoadJsonFromFile(this.jsonPath);
            try
            {
                // Split the path to find the parent object
                string[] keys = keyName.Split('.');
                JObject current = jsonData;

                // Navigate to the object that will contain the new key
                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else
                    {
                        // If any segment of the path does not exist, create new JObject containers
                        current[keys[i]] = new JObject();
                        current = (JObject)current[keys[i]];
                    }
                }

                // Add the new key with the provided value
                string newKey = keys[keys.Length - 1];
                if (!current.ContainsKey(newKey))
                {
                    current[newKey] = value;  // Directly add the string value
                    this.SaveJsonToFile(jsonData, this.jsonPath);
                }
                else
                {
                    MessageBox.Show($"Key '{newKey}' 已经存在");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败:" + ex.Message);
            }
        }
        public void AddKeyValue(string keyName, bool value)
        {
            JObject jsonData = LoadJsonFromFile(this.jsonPath);
            try
            {
                // Split the path to find the parent object
                string[] keys = keyName.Split('.');
                JObject current = jsonData;

                // Navigate to the object that will contain the new key
                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else
                    {
                        // If any segment of the path does not exist, create new JObject containers
                        current[keys[i]] = new JObject();
                        current = (JObject)current[keys[i]];
                    }
                }

                // Add the new key with the provided value
                string newKey = keys[keys.Length - 1];
                if (!current.ContainsKey(newKey))
                {
                    current[newKey] = value;  // Directly add the string value
                    this.SaveJsonToFile(jsonData, this.jsonPath);
                }
                else
                {
                    MessageBox.Show($"Key '{newKey}' 已经存在");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败:" + ex.Message);
            }
        }

        /// <summary>
        /// 修改键值
        /// </summary>
        /// <param name="keyName">键</param>
        /// <param name="value">值</param>
        public void ModifyValue(string keyName, JToken value)
        {

            JObject jsonData = this.LoadJsonFromFile(this.jsonPath);
            try
            {
                string[] keys = keyName.Split('.');
                JObject current = jsonData;

                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else if (current[keys[i]] is null || !(current[keys[i]] is JObject))
                    {
                        MessageBox.Show(keys[i] + "不存在");
                        return;
                    }
                }

                // Use compatible syntax for C# 7.3
               
                current[keys[keys.Length - 1]] = value;
                this.SaveJsonToFile(jsonData, this.jsonPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改失败:" + ex.Message);
            }
        }
        public void ModifyValue(string keyName, int value)
        {

            JObject jsonData = this.LoadJsonFromFile(this.jsonPath);
            try
            {
                string[] keys = keyName.Split('.');
                JObject current = jsonData;

                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else if (current[keys[i]] is null || !(current[keys[i]] is JObject))
                    {
                        MessageBox.Show(keys[i] + "不存在");
                        return;
                    }
                }

                // Use compatible syntax for C# 7.3
                current[keys[keys.Length - 1]] = value;
                this.SaveJsonToFile(jsonData, this.jsonPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改失败:" + ex.Message);
            }
        }

        public void ModifyValue(string keyName, float value)
        {

            JObject jsonData = this.LoadJsonFromFile(this.jsonPath);
            try
            {
                string[] keys = keyName.Split('.');
                JObject current = jsonData;

                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else if (current[keys[i]] is null || !(current[keys[i]] is JObject))
                    {
                        MessageBox.Show(keys[i] + "不存在");
                        return;
                    }
                }

                // Use compatible syntax for C# 7.3
                current[keys[keys.Length - 1]] = value;
                this.SaveJsonToFile(jsonData, this.jsonPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改失败:" + ex.Message);
            }
        }

        public void ModifyValue(string keyName, string value)
        {

            JObject jsonData = this.LoadJsonFromFile(this.jsonPath);
            try
            {
                string[] keys = keyName.Split('.');
                JObject current = jsonData;

                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else if (current[keys[i]] is null || !(current[keys[i]] is JObject))
                    {
                        MessageBox.Show(keys[i] + "不存在");
                        return;
                    }
                }

                // Use compatible syntax for C# 7.3
                current[keys[keys.Length - 1]] = value;
                this.SaveJsonToFile(jsonData, this.jsonPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改失败:" + ex.Message);
            }
        }

        public void ModifyValue(string keyName, bool value)
        {

            JObject jsonData = this.LoadJsonFromFile(this.jsonPath);
            try
            {
                string[] keys = keyName.Split('.');
                JObject current = jsonData;

                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else if (current[keys[i]] is null || !(current[keys[i]] is JObject))
                    {
                        MessageBox.Show(keys[i] + "不存在");
                        return;
                    }
                }

                // Use compatible syntax for C# 7.3
                current[keys[keys.Length - 1]] = value;
                this.SaveJsonToFile(jsonData, this.jsonPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改失败:" + ex.Message);
            }
        }

        /// <summary>
        /// 修改键名称
        /// </summary>
        /// <param name="oldKeyName"></param>
        /// <param name="newKeyName"></param>
        public void ModifyKeyName(string oldKeyName, string newKeyName)
        {
            JObject jsonData = this.LoadJsonFromFile(this.jsonPath);
            try
            {
                string[] keys = oldKeyName.Split(new char[] { '.' });
                JObject current = jsonData;

                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else if (current[keys[i]] is null || !(current[keys[i]] is JObject))
                    {
                        MessageBox.Show(keys[i] + "不存在");
                        return;
                    }
                }

                string originalKey = keys[keys.Length - 1];

                if (!current.ContainsKey(originalKey))
                {
                    MessageBox.Show(originalKey + "不存在");
                    return;
                }
                if (current.ContainsKey(newKeyName))
                {
                    MessageBox.Show(newKeyName + "已存在");
                    return;
                }

                JToken valueToMove = current[originalKey];
                current.Add(newKeyName, valueToMove);
                current.Remove(originalKey);

                this.SaveJsonToFile(jsonData, this.jsonPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 复制到同层
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="newKeyName"></param>
        public void copyKeyAtSameLevelWithNewName(string keyName, string newKeyName)
        {
            JObject jsonData = this.LoadJsonFromFile(this.jsonPath);

            try
            {
                string[] keys = keyName.Split(new char[] { '.' });
                JObject current = jsonData;

                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else if (current[keys[i]] is null || !(current[keys[i]] is JObject))
                    {
                        MessageBox.Show(keys[i] + "不存在");
                        return;
                    }
                }

                string originalKey = keys[keys.Length - 1];

                // Ensure the original key exists and the new key name does not already exist
                if (!current.ContainsKey(originalKey))
                {
                    MessageBox.Show(originalKey + "不存在");
                    return;
                }

                if (current.ContainsKey(newKeyName))
                {
                    MessageBox.Show(newKeyName + "已存在");
                    return;
                }

                // Clone the value of the original key
                JToken originalValue = current[originalKey].DeepClone();

                // Add the cloned value under the new key name
                current.Add(newKeyName, originalValue);
                this.SaveJsonToFile(jsonData, this.jsonPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="KeyName"></param>
        public void Delete(string KeyName)
        {
            JObject jsonData = this.LoadJsonFromFile(this.jsonPath);
            try
            {
                string[] keys = KeyName.Split(new char[] { '.' });
                JObject current = jsonData;
                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else if (current[keys[i]] is null || !(current[keys[i]] is JObject))
                    {
                        MessageBox.Show(keys[i] + "不存在");
                    }
                }

                if (!current.Remove(keys[keys.Length - 1]))
                {
                    MessageBox.Show(keys[keys.Length - 1] + "不存在或已删除");
                }
                else
                {
                    this.SaveJsonToFile(jsonData, this.jsonPath);
                }

            }
            catch (KeyNotFoundException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public T GetValue<T>(string keyName)
        {
            T t = default;
            JObject jsonData = this.LoadJsonFromFile(this.jsonPath);
            try
            {
                string[] keys = keyName.Split('.');
                JObject current = jsonData;

                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else if (current[keys[i]] is null || !(current[keys[i]] is JObject))
                    {
                        MessageBox.Show(keys[i] + "不存在");
                        return t;
                    }
                }
                // Use compatible syntax for C# 7.3
                JToken value = current[keys[keys.Length - 1]];
                t = value.ToObject<T>();

                return t;
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取失败:" + ex.Message);
                return t;
            }
        }

        public void AddKeyValueClass(string keyName)
        {
            JObject jsonData = LoadJsonFromFile(this.jsonPath);
            try
            {
                // Split the path to find the parent object
                string[] keys = keyName.Split('.');
                JObject current = jsonData;

                // Navigate to the object that will contain the new key
                for (int i = 0; i < keys.Length - 1; i++)
                {
                    if (current[keys[i]] is JObject next)
                    {
                        current = next;
                    }
                    else
                    {
                        // If any segment of the path does not exist, create new JObject containers
                        current[keys[i]] = new JObject();
                        current = (JObject)current[keys[i]];
                    }
                }

                // Add the new key with the provided value
                string newKey = keys[keys.Length - 1];
                if (!current.ContainsKey(newKey))
                {
                    current.Add(newKey, new JObject { });  // Directly add the string value
                    this.SaveJsonToFile(jsonData, this.jsonPath);
                }
                else
                {
                    MessageBox.Show($"Key '{newKey}' 已经存在");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败:" + ex.Message);
            }
        }

        public void AddJobject(string Comments, string keyName, JObject keyValuePairs)
        {
            JObject jsonData = LoadJsonFromFile(this.jsonPath);

            var jo = jsonData[Comments] as JObject;

            jo.Add(keyName, keyValuePairs);

            this.SaveJsonToFile(jsonData, this.jsonPath);
        }

        public string GetComments(string Comments, string keyName,string lang)
        {
            try
            {
                string comments;
                JObject jsonData = LoadJsonFromFile(this.jsonPath);
                JObject jc = jsonData[Comments] as JObject;
                JObject jl = jc[keyName] as JObject;
                comments = jl[lang].ToString();
                return comments;
            }
            catch (Exception ex)
            {
                return null;
            }
         
        }

    }
    public enum JValueType
    {
        Integer,
        Float,
        String,
        Boolean
    }
}
